package fr.fermeManager.application.views;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;

import com.jgoodies.forms.factories.DefaultComponentFactory;

import fr.fermeManager.application.controllers.ControllerUserSelection;
import fr.fermeManager.library.model.beans.UserFermeManager;

public class ViewUserSelection extends BasePanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnUser;
	private JButton btnAdmin;
	private JLabel label;
	
	private ControllerUserSelection currentController = new ControllerUserSelection();
	private JPanel panel_1;
	private JLabel lblUser;
	private JLabel lblLogin;
	private JPasswordField passwordField;
	private JTextField textField;

	/**
	 * Create the panel.
	 */
	public ViewUserSelection() {
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					verifierUser();
				}
			}
		});
		setPreferredSize(new Dimension(515, 320));
		setSize(new Dimension(510, 302));
		setMinimumSize(new Dimension(515, 320));

		setFont(new Font("Tahoma", Font.PLAIN, 11));
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "S\u00E9lection utilisateur", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new MigLayout("", "[grow][grow,center][grow]", "[grow][grow][grow]"));

		label = DefaultComponentFactory.getInstance().createLabel("");
		add(label, "cell 1 0");
		label.setIcon(new ImageIcon(ViewUserSelection.class.getResource("/img48x48/user.png")));

		panel_1 = new JPanel();
		panel_1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				verifierUser();
			}
		});
		panel_1.setBorder(new TitledBorder(null, "Identifiants :", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(panel_1, "cell 1 1,grow");
		panel_1.setLayout(new MigLayout("", "[89.00,center][grow]", "[grow][grow][]"));

		lblUser = new JLabel("User :");
		panel_1.add(lblUser, "cell 0 0,alignx center");

		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					verifierUser();
				}
			}
		});
		panel_1.add(textField, "cell 1 0,growx");
		textField.setColumns(10);

		lblLogin = new JLabel("Login :");
		panel_1.add(lblLogin, "cell 0 1,alignx center");

		passwordField = new JPasswordField();
		passwordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					verifierUser();
				}
			}
		});
		panel_1.add(passwordField, "cell 1 1,growx");

		btnAdmin = new JButton("S'identifier");
		btnAdmin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				verifierUser();
			}
		});
		btnAdmin.setIcon(new ImageIcon(ViewUserSelection.class.getResource("/img16x16/lock.png")));
		panel_1.add(btnAdmin, "cell 1 2,alignx right");

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Utilisation limit\u00E9e :", TitledBorder.LEADING, TitledBorder.TOP,
				null, null));
		add(panel, "cell 1 2,grow");
		panel.setLayout(new MigLayout("", "[grow][][][grow]", "[grow][][grow]"));

		btnUser = new JButton("Invit\u00E9");
		btnUser.setIcon(new ImageIcon(ViewUserSelection.class.getResource("/img16x16/ok.png")));
		panel.add(btnUser, "cell 1 1");
		btnUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectionnerUtilisateur();
			}
		});

	}

	protected void selectionnerUtilisateur() {
		UserFermeManager user = currentController.verifierUtilisateur(textField.getText(), new String(passwordField.getPassword()));
		if(user == null)
			return;
		currentController.selectionnerUtilisateur(user);
	}

	protected void verifierUser() {

		UserFermeManager user = currentController.verifierUtilisateur(textField.getText(), new String(passwordField.getPassword()));
		
		if(user == null)
			return;

		if (!user.isAuthentifie()) {
			JOptionPane.showMessageDialog(this, "Login ou mot de passe incorrecte !", "Erreur de connexion !", JOptionPane.WARNING_MESSAGE);
			return;
		}
		currentController.selectionnerUtilisateur(user);
	}
}
